package com.kg.sdk.network;

import android.util.Log;

import com.common.network.NetworkConfigData;
import com.common.network.request.HeaderSetting;
import com.common.network.request.RequestBase;
import com.kg.sdk.util.CommonUtil;
import com.kg.sdk.util.HMAC;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;

//com.kg.sdk.network.KPHeaderSetting
public class KPHeaderSetting extends HeaderSetting {
    String key = "Fq1K0vEmu*g&Vfzxdn6M!Cr*j2EHl1HB";
    public static String x_sid = "";
    public KPHeaderSetting(RequestBase requset){
        super(requset);
    }
    public KPHeaderSetting(){
        super();
    }
    public void initHeader(){
        String data = (String)mRequset.getObject();
        String signData = "";
        if(mRequset instanceof KPGetRequest){

        }else  if(mRequset instanceof KPPostRequest){
            try {
                JSONObject jsonObject = new JSONObject(data);
                signData = getSignData1(jsonObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Log.e("data"," sign1111 data is "+signData);

        try {
            signData = new String(signData.getBytes(),"UTF-8");
           // signData = URLEncoder.encode(signData, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.e("data"," sign data is "+signData);
        try {
            String sign = HMAC.getSignature(signData,key);
            Log.e("data"," sign data 11 is "+sign);
            this.setHeader(NetworkConfigData.TOKEN_KEY,sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String randomStr = CommonUtil.getRandomString(32);
        Log.e("data"," x_sid data 11 is "+randomStr);
        this.setHeader("x_sid",x_sid);
    }

    private String getSignData(JSONObject jsonObject){
        int len = jsonObject.length();
        String [] strList = new String[len];
        Iterator<String> headerkeys = jsonObject.keys();
        StringBuilder builder = new StringBuilder();
        boolean isFirst = true;
        while (headerkeys.hasNext()) {
            String headerkey = headerkeys.next();
            if(isFirst){
            }else{
                builder.append(NetworkConfigData.AT);
            }
            builder.append(headerkey);
            builder.append(NetworkConfigData.DY);
            try {
                String headerValue = jsonObject.getString(headerkey);
                builder.append(headerValue);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            isFirst = false;
        }
        return builder.toString();
    }

    private String getSignData1(JSONObject jsonObject){
        int len = jsonObject.length();
        ArrayList<String> strList = new ArrayList(len);
        Iterator<String> headerkeys = jsonObject.keys();
        StringBuilder builder = new StringBuilder();
        boolean isFirst = true;
        int i = 0;
        while (headerkeys.hasNext()) {
            String headerkey = headerkeys.next();
            strList.add(headerkey);
            i++;
        }
        Collections.sort(strList,String.CASE_INSENSITIVE_ORDER);
        builder.append(strList.get(0));
        builder.append(NetworkConfigData.DY);
        try {
            builder.append(jsonObject.getString(strList.get(0)));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for( i = 1;i < strList.size();i++){
            builder.append(NetworkConfigData.AT);
            builder.append(strList.get(i));
            builder.append(NetworkConfigData.DY);
            try {
                builder.append(jsonObject.getString(strList.get(i)));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return builder.toString();
    }
}
